package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.questionbank.DifficultyAdjustmentType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 试题难度实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：question_difficulty
 *
 * 管理试题的难度等级设置，包括难度等级、调整原因、调整类型等。
 * 支持试题难度的手动和自动调整。
 * 提供难度调整的完整历史记录和审计功能。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("试题难度表")
@SQLDelete(sql = "UPDATE question_difficulty SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class QuestionDifficulty {

    /**
     * 难度记录唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 试题ID
     */
    @NotNull(message = "试题ID不能为空")
    @Column(name = "question_id", nullable = false, unique = true)
    private Long questionId;

    /**
     * 当前难度等级
     */
    @NotNull(message = "难度等级不能为空")
    @Column(name = "difficulty_level", nullable = false)
    private Integer difficultyLevel;

    /**
     * 原难度等级
     */
    @Column(name = "previous_level")
    private Integer previousLevel;

    /**
     * 调整原因
     */
    @Size(max = 500, message = "调整原因长度不能超过500个字符")
    @Column(name = "reason", length = 500)
    private String reason;

    /**
     * 调整类型
     */
    @NotNull(message = "调整类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "adjustment_type", nullable = false, length = 20)
    private DifficultyAdjustmentType adjustmentType;

    /**
     * 调整时间
     */
    @NotNull(message = "调整时间不能为空")
    @Column(name = "adjust_time", nullable = false)
    private LocalDateTime adjustTime;

    /**
     * 调整人
     */
    @NotNull(message = "调整人不能为空")
    @Size(max = 50, message = "调整人长度不能超过50个字符")
    @Column(name = "adjust_by", nullable = false, length = 50)
    private String adjustBy;

    /**
     * 调整依据
     */
    @Column(name = "adjustment_basis", length = 1000)
    private String adjustmentBasis;

    /**
     * 算法建议难度
     */
    @Column(name = "algorithm_suggestion")
    private Integer algorithmSuggestion;

    /**
     * 人工确认难度
     */
    @Column(name = "manual_confirmed")
    private Integer manualConfirmed;

    /**
     * 统计通过率
     */
    @Column(name = "statistical_pass_rate")
    private Double statisticalPassRate;

    /**
     * 平均答题时间
     */
    @Column(name = "average_answer_time")
    private Integer averageAnswerTime;

    /**
     * 难度置信度
     */
    @Column(name = "difficulty_confidence", precision = 3, scale = 2)
    private Double difficultyConfidence;

    /**
     * 下次调整建议时间
     */
    @Column(name = "next_adjustment_suggestion")
    private LocalDateTime nextAdjustmentSuggestion;

    /**
     * 难度元数据，JSON格式
     */
    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata;

    /**
     * 记录创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 记录最后更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 创建人用户ID
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 最后更新人用户ID
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

}